# -*- coding: UTF-8 -*-
import requests;
import cookielib
from bs4 import BeautifulSoup
import re
from msina.msinalogin import doLogin
import thread

def comment(url, content):
        session = requests.session();
        session.cookies = cookielib.LWPCookieJar("cookies")
        session.cookies.load("cookies")
        detail = session.get(url).text
        doc=BeautifulSoup(detail, "html.parser")
        postUrl=doc.find("form")['action'];
        postData={
                  "srcuid":doc.find("input", {"name":"srcuid"})['value'],
                  "id":doc.find("input", {"name":"id"})['value'],
                  "rl":doc.find("input", {"name":"rl"})['value'],
                  "content":content
                  }
        result = session.post("http://weibo.cn"+postUrl, data=postData)
        session.close()
        print result.status_code

class MyPostTool():

    def get(self, pageId, content):
        self.session=requests.session();
        self.session.cookies=cookielib.LWPCookieJar("cookies")
        self.session.cookies.load("cookies")
        try:
            mainpage = "http://weibo.cn/u/" +pageId+ "?vt=4"
            wb = self.session.get(mainpage)
            self.session.close()
            if wb.status_code != 200:
                doLogin()
                self.get(pageId, content)
                
            r = BeautifulSoup(wb.text, "html5lib")
            divs=r.findAll("div", {"class":"c", "id":re.compile("M_(\w)?")})
            for div in divs:
                thread.start_new_thread(comment, (div.find("a", {"class":"cc"})['href'], "345"))
        except Exception,e:
            print Exception,":",e
            pass   
    
    
               
postTool = MyPostTool()
postTool.get("2374056384", "@正蓝钱包")